<?php
/*
 *  Maniter: A webbased business managment tool.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * purchaseoffer.class.php, Maniter
 * 
 * This class is very mutch like the purchaseorder, but it also has a
 * header text.
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package maniter_classes
 */

require_once( 'purchaselist.class.php' );
require_once( 'pdfpurchase.class.php' );
require_once( _PATH_TO_JIBA.'dynamictext.class.php' );

class PurchaseOffer extends PurchaseList {
  /**
    * The footer text
    *
    * @var DynamicText
    */
  private $_footerText;
  
  /**
    * The header text
    *
    * @var DynamicText
    */
  private $_headerText;
	
  public static function &getPurchaseOffer( $id ){
    $db = DB::get();  //it appears like on some servers the database should be initialized before
                      //using mysql_real_escape_string, I don't know why this is
    $id = mysql_real_escape_string( $id );
    $res = $db->getRows( 'purchase_offers', "id='$id'" );
    
    $offer = new PurchaseOffer( $res[0]['id'] );
    $offer->getTimestamp()->setMysqlTimestamp( $res[0]['date'], true );
    $offer->setFooterText( $res[0]['footer'] );
    $offer->setHeaderText( $res[0]['header'] );
    
    $res[0]['finished'] ? $offer->setFinished() : $offer->unSetFinished();
    $res[0]['printed'] ? $offer->setPrinted() : '';
    
    $offer = PurchaseList::_getPurchases( $offer );
    $offer = PurchaseList::_getClientInfo( $offer );
    
    return $offer;
  }
  
  /**
    * Get purchaseorders out of the database.
    *
    * @param	int	$offset		start from
    * @param	int	$limit		limit results to
    * @return	PurchaseOrder[]
    * @access	public
    */
  public static function &getPurchaseOffers( $offset=0, $limit=0 ){
    $res = DB::get()->getRows( 'purchase_offers', null, 'date', false, $limit, $offset );
    
    $offers = array();
    $num    = count( $res );
    $total  = DB::get()->count( 'purchase_offers' );
    
    for( $i=0 ; $i<$num ; $i++ )
    {
      $offers[$i] = new PurchaseOffer( $res[$i]['id'] );
      $offers[$i]->setDispId( $total-$offset-$i );
      $offers[$i]->getTimestamp()->setMysqlTimestamp( $res[$i]['date'] );
      $offers[$i]->setFooterText( $res[$i]['footer'] );
      $offers[$i]->setHeaderText( $res[$i]['header'] );
      
      $res[$i]['finished'] ? $offers[$i]->setFinished() : $offers[$i]->unSetFinished();
      $res[$i]['printed'] ? $offers[$i]->setPrinted() : '';
      
      $offers[$i] = PurchaseList::_getPurchases( $offers[$i] );
      $offers[$i] = PurchaseList::_getClientInfo( $offers[$i] );
    }
    
    return $offers;
  }
	
  /**
    * Constructor: initializes variables and objects
    * + creates a database entry for this purchaseorder.
    */
  public function __construct( $id='' ) {
    $db = DB::get();
    
    parent::__construct();
    
    if( strlen($id) == 32 )
            $this->_id = $id;
    
    $this->_footerText = new DynamicText();
    $this->_headerText = new DynamicText();
    
    $this->_footerText->setText( $db->getItem('var_blob','value',"name='offerfootertext'") );
    $this->_headerText->setText( $db->getItem('var_blob','value',"name='offerheadertext'") );
  }
	
  /**
    * Call the parent function + add $_dispId to the array
    *
    * @return string
    */
  public function __sleep() {
    return array_merge( parent::__sleep(), array('_headerText','_footerText') );
  }
	
  public function __wakeup(){
          
  }
  
  /**
    * Set the footer text
    *
    * @param string $str
    */
  public function setFooterText( $str ){
    if( is_string($str) )
      $this->_footerText->setText($str);
  }
	
	/**
	 * Returns the unparsed footer
	 *
	 * @return string
	 */
	public function getFooter(){
		return $this->_footerText->getText();
	}
	
	public function parseFooter( $nl2br=true ){
		return $this->_footerText->parseText( $nl2br );
	}
	
	/**
	 * Set the header text
	 *
	 * @param string $str
	 */
	public function setHeaderText( $str ){
		if( is_string($str) )
			$this->_headerText->setText($str);
	}
	
	/**
	 * Returns the unparsed header
	 *
	 * @return string
	 */
	public function getHeader(){
		return $this->_headerText->getText();
	}
	
	public function parseHeader( $nl2br=true ){
		return $this->_headerText->parseText( $nl2br );
	}
	
	/**
	 * Generates a pdf for this purchase order.
	 * 
	 * Make sure you don't output anything else before
	 * or after you call this function!
	 *
	 */
	public function pdf(){
		$pdf = new PdfPurchase();
		
		$pdf->setPurchases( $this->_purchases );
		$pdf->setClientInfo( $this->_clientInfo );
		$pdf->setDate( $this->getTimestamp()->show() );
		$pdf->setDueDate( $this->getDueDate() );
		$pdf->setHeaderText( $this->parseHeader(false) );
		$pdf->setFootText( $this->parseFooter(false) );
		$pdf->setNumber( $this->_orderId );
		$pdf->setVatPerVat( $this->getVatPerVat(true) );
		$pdf->setSubtotal( $this->printSubtotal() );
		$pdf->setTotal( $this->printTotal() );
		
		$pdf->Output();
	}
	
	public static function save( $offer ){
          $db = DB::get();
          
          //saves purchases
          PurchaseList::save( $offer );
          
          $data = array(  'id' => $offer->_id,
                          'date' => $offer->getTimestamp()->getMysqlTimestamp(),
                          'printed' => $offer->printed() ? 1 : 0,
                          'finished' => $offer->_finished ? 1 : 0,
                          'footer' => $offer->_footerText->getText(),
                          'header' => $offer->_headerText->getText() );
  
          $db->save( 'purchase_offers', $data, true );
	}
}

?>